﻿using Google.GData.Client;
using Google.YouTube;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using Vlc.DotNet.Core.Medias;

namespace StreamersBestFriend
{
    class YoutubeVideo
    {
        public String title { get; internal set; }

        public bool restricted { get; internal set; }

        public LocationMedia media { get; internal set; }

        public YoutubeVideo(String url)
        {
            //retrieve video information
            Video _video = GetMediaData(url);

            //set video information in metadata
            title = _video.Title;
            restricted = checkRestrictions(_video);

            //set media
            media = new LocationMedia(url);
        }

        /// <summary>
        /// function to get metadata from a video using the youtube url
        /// </summary>
        /// <param name="url">the normalized youtube url</param>
        /// <returns>the video object containing the data</returns>
        private static Video GetMediaData(String url)
        {
            string URL = String.Empty;
            Video video = new Video();

            string gdata = @"https://gdata.youtube.com/feeds/api/videos/";

            try
            {
                YouTubeRequest request = new YouTubeRequest(new YouTubeRequestSettings(null, null, null));

                string paramV = System.Web.HttpUtility.ParseQueryString(new Uri(url).Query).Get("v");
                Uri videoEntryUrl = new Uri(gdata + paramV);
                video = request.Retrieve<Video>(videoEntryUrl);

                // videoEntryUrl:
                // http://www.youtube.com/watch?v=rnt4paSg2pI - requested by user
                // http://gdata.youtube.com/feeds/api/videos?q=rnt4paSg2pI - requested by api
                // http://gdata.youtube.com/feeds/api/videos?q=<id> - required
                if (video != null)
                {
                    foreach (Google.GData.YouTube.MediaContent i in video.Contents)
                    {
                        Debug.WriteLine(i.Value);
                    }

                    Debug.WriteLine(video.Title + " was added to the Queue");
                }

            }
            catch (System.IO.FileLoadException x)
            {
                Debug.WriteLine(x.FileName);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.StackTrace);
            }

            return video;
        }

        /// <summary>
        /// checks if a video is restricted and therefor cant be played
        /// </summary>
        /// <param name="pVideo">the video object containing the data</param>
        /// <returns>true if yes, false if no</returns>
        private static bool checkRestrictions(Video pVideo)
        {
            if (pVideo.Status != null)
            {
                if (pVideo.Status.Name.Equals("restricted"))
                {
                    return true;
                }                
            }
            return false;
        }
    }
}
